package com.anolesoft.twelveFactory.planManagement.service.impl;

import com.anolesoft.system.fac.entity.Employee;
import com.anolesoft.system.fac.service.IEmployeeService;
import com.anolesoft.twelveFactory.planManagement.entity.PlanIssued;
import com.anolesoft.twelveFactory.planManagement.entity.Process;
import com.anolesoft.twelveFactory.planManagement.mapper.PlanIssuedMapper;
import com.anolesoft.twelveFactory.planManagement.mapper.ProcessMapper;
import com.anolesoft.twelveFactory.planManagement.service.ProcessService;
import com.anolesoft.yr.util.PageData;
import com.anolesoft.yr.util.SecurityUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProcessServiceImpl extends ServiceImpl<ProcessMapper,Process> implements ProcessService {

    @Resource
    private ProcessMapper processMapper;
    @Resource
    private PlanIssuedMapper planIssuedMapper;

    @Autowired
    private IEmployeeService iEmployeeService;

    @Override
    public List<Process> selectForJx() {
        return this.processMapper.selectForJx();
    }


    @Override
    public PageData<List<Process>> listProcess(Process process) {
        Page<Process> page = new Page<Process>(process.getPageNo(), process.getPageSize());
        List<Process> listProcess=processMapper.listProcess(process,page);
        return new PageData<>(((int) page.getTotal()), listProcess);
    }


    //计划下达页面所有数据
    @Override
    public PageData<List<Process>> listPlanlsssued(Process process) {
        Page<Process> page = new Page<Process>(process.getPageNo(), process.getPageSize());
        List<Process> listProcess=processMapper.selectCheckplan(process,page);
        for (int i = 0; i <listProcess.size(); i++) {
            if (listProcess.get(i).getFth()==null){
                listProcess.get(i).setWyzd(listProcess.get(i).getJx()+",,"+listProcess.get(i).getTh()+","+listProcess.get(i).getJfcj()+"-"+listProcess.get(i).getJscj());
            }
            else {
                listProcess.get(i).setWyzd(listProcess.get(i).getJx()+",,"+listProcess.get(i).getFth()+"::"+listProcess.get(i).getTh()+","+listProcess.get(i).getJfcj()+"-"+listProcess.get(i).getJscj());
            }
            listProcess.get(i).setJhzt("未下达");
            String a=listProcess.get(i).getClph()==null?"":listProcess.get(i).getClph();
            String b=listProcess.get(i).getClgg()==null?"":listProcess.get(i).getClgg();
            if(a.equals("")&&!b.equals("")){
                listProcess.get(i).setXl(b);
            }
            else if(!a.equals("")&&b.equals("")){
                listProcess.get(i).setXl(a);
            }
            else if(a.equals("")&&b.equals("")){
                listProcess.get(i).setXl("");
            }
            else {
                listProcess.get(i).setXl(a + "-" + b);
            }
            listProcess.get(i).setWg(listProcess.get(i).getDzjdh());
//            listProcess.get(i).setDjs(listProcess.get(i).getDj());
            this.processMapper.update(listProcess.get(i));

        }
        //父图号为空
        List<Process> roots=listProcess.stream().filter(l -> l.getFth()==null).collect(Collectors.toList());

        List<Process> processes = new ArrayList<>();
        for (int i = 0; i < listProcess.size(); i++) {
            Process p = this.processMapper.selectByWYZD((listProcess.get(i)).getWyzd());
            while(true) {
                if (p != null) {
                    processes.add(p);
                }

                if (p == null || p.getFth() == null) {
                    break;
                }

                p = this.processMapper.selectByTh(p.getFth());
            }
        }

//                List<Process> processes1 = processes.stream().distinct().collect(Collectors.toList());

//        得到id
        List<String> id = processes.stream().map(Process::getObjuid).collect(Collectors.toList());


        roots = roots.stream().filter(l -> {
            return id.contains(l.getObjuid());
        }).collect(Collectors.toList());

        roots.forEach(root -> {
            Process.setChildren(processes, root);
        });

//        sort() 方法用于对数组的元素进行排序。
        roots.sort((e1, e2) -> {
            return -e1.compareTo(e2);
        });

        for (int i = 0; i < roots.size(); i++) {
//            码  .前
            String code = String.valueOf(i + 1);
            roots.get(i).setCode(code);
            List<Process> children = roots.get(i).getChildren();
            if (children != null && !children.isEmpty()) {
                //            码  .前+后
                Process.setChildrenCode(children, code);
            }
        }
        return new PageData<>(((int) page.getTotal()), roots);
    }
    @Override
    public Process selectById(String objuid) {
        return this.processMapper.selectById(objuid);
    }

    @Override
    public Integer update(Process process) {
        //修改前
        Process process1=this.processMapper.selectById(process.getObjuid());
        this.processMapper.update(process);
        //修改后
        Process process2=this.processMapper.selectById(process.getObjuid());
        boolean a = false;
        if(process2.getClph()!=null){
            if(process1.getClph()!=null){
                if(!process1.getClph().equals(process2.getClph())){
                    a = true;
                }
            }else{
                a = true;
            }
        }else{
            if(process1.getClph()!=null){
                a = true;
            }
        }

        if(process2.getClgg()!=null){
            if(process1.getClgg()!=null){
                if(!process1.getClgg().equals(process2.getClgg())){
                    a = true;
                }
            }else{
                a = true;
            }
        }else{
            if(process1.getClgg()!=null){
                a = true;
            }
        }

        if(process2.getDzjdh()!=null){
            if(process1.getDzjdh()!=null){
                if(!process1.getDzjdh().equals(process2.getDzjdh())){
                    a = true;
                }
            }else{
                a = true;
            }
        }else{
            if(process1.getDzjdh()!=null){
                a = true;
            }
        }
        if(process2.getJx()!=null){
            if(process1.getJx()!=null){
                if(!process1.getJx().equals(process2.getJx())){
                    a = true;
                }
            }else{
                a = true;
            }
        }else{
            if(process1.getJx()!=null){
                a = true;
            }
        }
        if(process2.getDj()!=null){
            if(process1.getDj()!=null){
                if(!process1.getDj().equals(process2.getDj())){
                    a = true;
                }
            }else{
                a = true;
            }
        }else{
            if(process1.getDj()!=null){
                a = true;
            }
        }
        if(process2.getNbfg()!=null){
            if(process1.getNbfg()!=null){
                if(!process1.getNbfg().equals(process2.getNbfg())){
                    a = true;
                }
            }else{
                a = true;
            }
        }else{
            if(process1.getNbfg()!=null){
                a = true;
            }
        }
        if(process2.getGylx()!=null){
            if(process1.getGylx()!=null){
                if(!process1.getGylx().equals(process2.getGylx())){
                    a = true;
                }
            }else{
                a = true;
            }
        }else{
            if(process1.getGylx()!=null){
                a = true;
            }
        }
        if(process2.getLjlx()!=null){
            if(process1.getLjlx()!=null){
                if(!process1.getLjlx().equals(process2.getLjlx())){
                    a = true;
                }
            }else{
                a = true;
            }
        }else{
            if(process1.getLjlx()!=null){
                a = true;
            }
        }
        if(a==true){
            process2.setXgzt(1);
        }
         Integer I=this.processMapper.update(process2);
        return I;
    }

    @Override
    public Integer Insert(Process process) {
        process.setCjsj(Date.valueOf(LocalDate.now()));
        process.setCjr(SecurityUtils.getLoginUser().getUsername());
        Employee employee = (Employee)this.iEmployeeService.getById(SecurityUtils.getLoginUser().getEmpUid());
        if(employee!=null) {
            if (employee.getFacCode() != null) {
                process.setCdh(employee.getFacCode());
            }
        }
        PlanIssued planIssued=new PlanIssued();
        planIssued.setWg(process.getDzjdh());
        planIssued.setXl(process.getClph()+"-"+process.getClgg());
        planIssued.setJx(process.getJx());
        planIssued.setDjs(process.getDj());
        planIssued.setNbfg(process.getNbfg());
        planIssued.setGylx(process.getGylx());
        planIssued.setLjlx(process.getLjlx());
        planIssued.setFth(process.getFth());
        planIssued.setTh(process.getTh());
        planIssued.setObjuidgyjh(process.getObjuid());
        planIssued.setWyzd(process.getWyzd());
        this.planIssuedMapper.insert(planIssued);
        return this.processMapper.Insert(process);
    }

    @Override
    public Integer delete(String objuid) {
        List<PlanIssued> planIssued=this.planIssuedMapper.selectByObjuidgyjh(objuid);
        for (int i = 0; i < planIssued.size(); i++) {
            if(planIssued.get(i).getJhzt().equals("审核通过")){
                planIssued.get(i).setJhzt("删除");
                planIssued.get(i).setJhbj("删除");
                this.planIssuedMapper.Update(planIssued.get(i));
            }
        }
        return this.processMapper.deleteById(objuid);
    }
}
